﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication.Code;
using Telerik.Web.UI;
using Company.Merchandising.BusinessLayer.DataEntities;
using Company.Merchandising.Utilities.LinqToEntities;
using Company.Merchandising.BusinessLayer.DataTransferObjects;

namespace WebApplication.Controls.Management.Company
{
	public partial class ContactPersonEditControl : EntityUserControl
	{
		protected void Page_Init(object sender, EventArgs e)
		{
			GridEditableItem item = this.Parent.NamingContainer as GridEditableItem;
			if (item == null || item.DataItem == null)
			{
				return;
			}

			if (item.DataItem is GridInsertionObject)
			{
				btUpdate.Visible = false;
				return;
			}

			ContactPerson editedContactPeron = EntityDataSourceExtensions.GetItemObject<ContactPerson>(item.DataItem);
			if (editedContactPeron != null)
			{
				btInsert.Visible = false;
				SetContactPersonValues(editedContactPeron);
				return;
			}
			return;
		}

		public DTOContactPerson EditedContactPerson
		{
			get
			{
				return new DTOContactPerson()
				{
					Name = NameTextBox.Text,
					Surname = SurnameTextBox.Text,
					Description = DescriptionTextBox.Text,
					Email = EmailTextBox.Text,
					Phone = PhoneTextBox.Text,
					MobilePhone = MobilePhoneTextBox.Text,
					Fax = FaxTextBox.Text
				};
			}
		}

		private void SetContactPersonValues(ContactPerson contactPerson)
		{
			NameTextBox.Text = contactPerson.Name;
			SurnameTextBox.Text = contactPerson.Surname;
			DescriptionTextBox.Text = contactPerson.Description;
			EmailTextBox.Text = contactPerson.Email;
			PhoneTextBox.Text = contactPerson.Phone;
			MobilePhoneTextBox.Text = contactPerson.MobilePhone;
			FaxTextBox.Text = contactPerson.Fax;
		}
	}
}